home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / demos / addtest.c next >
Encoding:
C/C++ Source or Header  |  2002-03-27  |  5.1 KB  |  297 lines

  1. #include <stdlib.h>
  2. #include <mgl/gl.h>
  3.  
  4. #include <math.h>
  5. #ifndef M_PI
  6. #define M_PI 3.14159265
  7. #endif
  8. #include <stdio.h>
  9.  
  10. GLint width=640; GLint height=480;
  11. GLfloat startilt = 0.0;
  12. GLfloat t_off = 0.0;
  13. GLfloat fov = 170.0;
  14. int x, y;
  15.  
  16. typedef struct
  17. {
  18.     GLfloat x,y,z,u,v;
  19. } MyVertex;
  20.  
  21.  
  22. GLubyte *LoadPPM(char *name, GLint *w, GLint *h)
  23. {
  24.     int i, j;
  25.     unsigned long x,y;
  26.     FILE *f;
  27.     GLubyte *where;
  28.     GLubyte *cur;
  29.  
  30.     f = fopen(name, "r");
  31.  
  32.     if (!f)
  33.     {
  34.     *w = 0; *h=0;
  35.     return NULL;
  36.     }
  37.     #ifndef __STORM__
  38.     i = fscanf(f, "P6\n%ld %ld\n255\n",&x, &y);
  39.     #else
  40.     i = fscanf(f, "P6\n%ld\n%ld\n255\n", &x, &y);
  41.     #endif
  42.  
  43.     if (i!= 2)
  44.     {
  45.     printf("Error scanning PPM header\n");
  46.     fclose(f);
  47.     *w = 0; *h = 0;
  48.     return NULL;
  49.     }
  50.  
  51.     *w = x;
  52.     *h = y;
  53.  
  54.     where = malloc(x*y*4);
  55.     if (!where)
  56.     {
  57.     printf("Error out of Memory\n");
  58.     fclose(f);
  59.     *w = 0; *h = 0;
  60.     return NULL;
  61.     }
  62.  
  63.     cur = where;
  64.     for (j = 0; j < x*y; j++)
  65.     {
  66.     fread(cur, 1, 1, f); cur++;
  67.     fread(cur, 1, 1, f); cur++;
  68.     fread(cur, 1, 1, f); cur++;
  69.     *cur++ = 100;
  70.     }
  71. #if 0
  72.     i = fread(where, 1, x*y*3, f);
  73.     fclose(f);
  74.  
  75.     if (i != x*y*3)
  76.     {
  77.     printf("Error while reading file\n");
  78.     free(where);
  79.     *w = 0; *h = 0;
  80.     return NULL;
  81.     }
  82. #endif
  83.     return where;
  84. }
  85.  
  86.  
  87. BOOL TexInit(char *name, int num)
  88. {
  89.     GLubyte *tmap;
  90.     GLint x,y;
  91.  
  92.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  93.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  94.  
  95.     if (!name)
  96.     {
  97.     return FALSE;
  98.     }
  99.     else
  100.     {
  101.     tmap = LoadPPM(name, &x, &y);
  102.     }
  103.  
  104.     if (!tmap)
  105.     return FALSE;
  106.  
  107.     glBindTexture(GL_TEXTURE_2D, num);
  108.     glTexImage2D(GL_TEXTURE_2D, 0, 4,
  109.     x,y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmap);
  110.     free(tmap);
  111.  
  112.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  113.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  114.  
  115.     glEnable(GL_TEXTURE_2D);
  116.  
  117.     return TRUE;
  118. }
  119.  
  120. static void drawFlare(GLint texnum)
  121. {
  122.     GLfloat w,h;
  123.  
  124.     w=width/10.0;
  125.     h=w;
  126.  
  127.  
  128.     glBindTexture(GL_TEXTURE_2D, texnum);
  129.     glBlendFunc(GL_ONE, GL_ONE);
  130.     glEnable(GL_BLEND);
  131.     /*
  132.     ** An MGL_FLATFAN is a shortcut to draw something into the
  133.     ** screen that does not go through the transformation pipeline.
  134.     ** This is also not clipped, so you have to take care to avoid
  135.     ** going over the edges, or clip yourself.
  136.     */
  137.     glBegin(MGL_FLATFAN);
  138.     glTexCoord2f(0.0, 0.0);
  139.     glVertex2f(x,y);
  140.     glTexCoord2f(1.0, 0.0);
  141.     glVertex2f(x+w, y);
  142.     glTexCoord2f(1.0, 1.0);
  143.     glVertex2f(x+w,y+h);
  144.     glTexCoord2f(0.0, 1.0);
  145.     glVertex2f(x,y+h);
  146.     glEnd();
  147.     glDisable(GL_BLEND);
  148. }
  149.  
  150.  
  151. void reshape(int w, int h)
  152. {
  153.     glMatrixMode(GL_PROJECTION);
  154.     glLoadIdentity();
  155.     gluPerspective(70.0, 1.3333333, 1.0, 100.0);
  156.  
  157.     glMatrixMode(GL_MODELVIEW);
  158.     glViewport(0, 0, w, h);
  159.     glClearColor(0.0f, 0.f, 0.f, 1.f);
  160.     glClearDepth(1.0);
  161. }
  162.  
  163. void DoFrame(void)
  164. {
  165. //    glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  166.  
  167.     glMatrixMode(GL_PROJECTION);
  168.     glLoadIdentity();
  169.     gluPerspective(fov, 1.3333333, 1.0, 100.0);
  170.  
  171.     glMatrixMode(GL_MODELVIEW);
  172.     glLoadIdentity();
  173.  
  174.  
  175.     drawFlare(2);
  176.  
  177.     startilt += 0.1;
  178.     t_off += 0.007;
  179.  
  180.     mglSwitchDisplay();
  181. }
  182.  
  183.  
  184. void keys(char c)
  185. {
  186.  
  187.     switch (c)
  188.     {
  189.     case 0x1b:
  190.         mglExit();
  191.         break;
  192.     case 'w':
  193.         y-=5;
  194.         break;
  195.     case 's':
  196.         y+=5;
  197.         break;
  198.     case 'q':
  199.         x-=5;
  200.         break;
  201.     case 'e':
  202.         x+=5;
  203.         break;
  204.     case '1':
  205.         glDisable(GL_BLEND);
  206.         break;
  207.     case '2':
  208.         glEnable(GL_BLEND);
  209.         break;
  210.     }
  211.  
  212. }
  213.  
  214. int main(int argc, char *argv[])
  215. {
  216.     int i;
  217.     char *filename = "data/stars.ppm";
  218.     char *flarename = "data/flare.ppm";
  219.  
  220.     for (i=1; i<argc; i++)
  221.     {
  222.     if (0 == stricmp(argv[i], "-width"))
  223.     {
  224.         i++;
  225.         width = atoi(argv[i]);
  226.     }
  227.     if (0 == stricmp(argv[i], "-height"))
  228.     {
  229.         i++;
  230.         height = atoi(argv[i]);
  231.     }
  232.     if (0 == stricmp(argv[i], "-window"))
  233.     {
  234.         mglChooseWindowMode(GL_TRUE);
  235.     }
  236.     if (0 == stricmp(argv[i], "-texture"))
  237.     {
  238.         i++;
  239.         filename = argv[i];
  240.     }
  241.     if (0 == stricmp(argv[i], "-flare"))
  242.     {
  243.         i++;
  244.         flarename = argv[i];
  245.     }
  246.     }
  247.  
  248.     MGLInit();
  249.  
  250.     mglChooseVertexBufferSize(1000);
  251.     mglChooseNumberOfBuffers(2);
  252.     mglCreateContext(0,0,width,height);
  253.     mglEnableSync(GL_FALSE);
  254.  
  255.     glClearColor(0.0, 0.0, 0.0, 0.0);
  256.     reshape(width, height);
  257.     glDisable(GL_DEPTH_TEST);
  258.  
  259.     x=width/2.0;
  260.     y=height/2.0;
  261.  
  262.     glClearColor(0.3, 0.3, 0.3, 0.3);
  263.     mglSwitchDisplay();
  264.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  265.     mglSwitchDisplay();
  266.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  267.     mglSwitchDisplay();
  268.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  269.  
  270.  
  271.     if ( TexInit(filename, 1) && TexInit(flarename, 2) )
  272.     {
  273.     glClearColor(0.0, 0.0, 0.0, 1.0);
  274.     glColor3f(1.0, 1.0, 1.0);
  275.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  276.  
  277.     glHint(MGL_W_ONE_HINT, GL_FASTEST);
  278.     glEnable(GL_TEXTURE_2D);
  279.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  280.  
  281.  
  282.     mglLockMode(MGL_LOCK_SMART);
  283.     mglIdleFunc(DoFrame);
  284.     mglKeyFunc(keys);
  285.     mglMainLoop();
  286.     }
  287.     else
  288.     {
  289.     printf("Can't load textures\n");
  290.     }
  291.  
  292.     mglDeleteContext();
  293.     MGLTerm();
  294.     return 0;             /* ANSI C requires main to return int. */
  295. }
  296.  
  297.